﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace iControl.Controls
{
	/// <summary>
	/// Interaction logic for Rating.xaml
	/// </summary>
	public partial class Rating: UserControl
	{
		#region Variable Declaration
		private const int MaxValue = 5;
		#endregion

		#region Constructor
		public Rating()
		{
			InitializeComponent();			
		}
		#endregion

		#region Event Declaration
		public static readonly RoutedEvent RatingChangedEvent = EventManager.RegisterRoutedEvent("RatingChanged", 
			RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Rating));
		public event RoutedEventHandler RatingChanged
		{
			add { AddHandler(RatingChangedEvent, value); }
			remove { RemoveHandler(RatingChangedEvent, value); }
		}
		protected void OnRatingChanged()
		{
			RaiseEvent(new RoutedEventArgs(Rating.RatingChangedEvent));
		}
		#endregion

		#region Event Handlers
		private void Stars_Click(Object sender, RoutedEventArgs e)
		{
			ToggleButton button = sender as ToggleButton;
			int newRating = int.Parse((String)button.Tag);

			if( (bool)button.IsChecked || newRating < RatingValue )
			{
				RatingValue = newRating;
			}
			else
			{
				RatingValue = newRating - 1;
			}

			e.Handled = true;
			OnRatingChanged();
		}

		private static void RatingValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
		{
			Rating parent = sender as Rating;
			int ratingValue = (int)e.NewValue;
			UIElementCollection children = ((Grid)(parent.Content)).Children;
			ToggleButton button = null;

			for( int i = 0; i < ratingValue; i++ )
			{
				button = children[i] as ToggleButton;
				if( button != null )
					button.IsChecked = true;
			}

			for( int i = ratingValue; i < children.Count; i++ )
			{
				button = children[i] as ToggleButton;
				if( button != null )
					button.IsChecked = false;
			}						
		}
		#endregion

		#region Dependency Properties
		public static readonly DependencyProperty RatingValueProperty = DependencyProperty.Register("RatingValue", typeof(int), 
			typeof(Rating), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
			new PropertyChangedCallback(RatingValueChanged)));		
		#endregion

		#region Properties
		public int RatingValue
		{
			get { return (int)GetValue(RatingValueProperty); }
			set
			{
				if( value < 0 )
				{
					SetValue(RatingValueProperty, 0);
				}
				else if( value > MaxValue )
				{
					SetValue(RatingValueProperty, MaxValue);
				}
				else
				{
					SetValue(RatingValueProperty, value);
				}
			}
		}
		#endregion									
	}
}
